<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Config.class.php";
require_once dirname(__FILE__)."/ObjectContainer.class.php";
require_once dirname(__FILE__)."/Localization.class.php";
require_once dirname(__FILE__)."/LocaleContainer.class.php";
require_once dirname(__FILE__)."/SessionRoster.class.php";
require_once dirname(__FILE__)."/Wowhead.class.php";

/**
 * Used to display crafts on the website.
 * 
 * @author Emmanuel Cron
 *
 */
class CraftDisplayer
{	
	/** Single instance of this class. */
	static private $instance;
	
	private $reagent_max_characters_default = 13;
	private $path_to_question_mark = "Interface/Icons/INV_Misc_QuestionMark";
	
	private $log;
	
	private function __construct()
	{
		$this->log = new Log('craft_displayer');
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new CraftDisplayer();
		}
		
		return self::$instance;
	}
	
	function Name($craft)
	{
		$string = null;
		$object = null;
		$link = null;
		
		if ($craft->IsItem()) {
			// The craft is an item
			$object = ObjectContainer::GetInstance()->GetItem($craft->GetItemId());
			
			if ($object == null) {
				// Item not found
				
				$this->log->AddError("Could not find item " . $craft->GetItemId() . " for craft: " . $craft->GetUniqueId());
				return "<img src='' title='".$this->path_to_question_mark.".png' alt='' style='padding-right: 5px; vertical-align: middle;' /> <em>&lt;".Localization::GetInstance()->Get('unknown craft')."&gt;</em>";
			}
		} else {
			// The craft is a spell
			$object = ObjectContainer::GetInstance()->GetSpell( $craft->GetSpellId() );
			
			if ($object == null) {
				// Spell not found
				
				$this->log->AddError("Could not find spell " . $craft->GetSpellId() . " for craft: " . $craft->GetUniqueId());
				return "<img src='' title='".$this->path_to_question_mark.".png' alt='' style='padding-right: 5px; vertical-align: middle;' /> <em>&lt;".Localization::GetInstance()->Get('unknown craft')."&gt;</em>";
			}
			
			$link = str_replace("%s", $object->GetSpellId(), Wowhead::GetInstance()->GetSpellLinkByLocale(SessionRoster::GetInstance()->GetLocalization()));
		}
		
		// Display the icon
		$object_img = file_exists(dirname(__FILE__)."/../".$object->GetTexture().".png") ? $object->GetTexture() : $this->path_to_question_mark;
		$string .= "<img src='' title='".$object_img.".png' alt='' style='float: left; padding-right: 5px; vertical-align: middle;' /> ";
		
		// Display the current locale if it has one
		if ( $craft->HasNameThisLocale() ) {
			$string .= "<a href='".$this->GetObjectLink($object, $craft->IsItem())."'><span style='color: #".$object->GetColor().";' >".utf8_encode($craft->GetNameThisLocale())."</span></a>";
		}
		
		// Display the other locales
		foreach ( $craft->GetNamesOtherLocales() as $locale => $name ) {
			$string .= "<br /><a href='".$this->GetObjectLink($object, $craft->IsItem(), $locale)."'><em style='color: #".$object->GetColor().";'>".utf8_encode($name)."</em></a>";
		}
		
		return $string;
	}
	
	/**
	 * Returns the link that should be used
	 * to represent an object. It is based on
	 * the given object, the fact that it is
	 * an item or not, and the locale to use.
	 * <p>
	 * If no locale is specified, then the
	 * locale currently used by the user is
	 * selected.
	 * 
	 * @param $object the object to get the link
	 * @param $isItem <code>true</code> if the object
	 * 		is an item, <code>false</code> otherwise
	 * @param $locale the locale to use to
	 * 		determine which link to return
	 * @return a link to the object
	 */
	private function GetObjectLink($object, $isItem, $locale=null) {
		if ($locale == null) {
			$locale = SessionRoster::GetInstance()->GetLocalization();
		}
		
		if ($isItem) {
			return str_replace("%s", $object->GetItemId(), Wowhead::GetInstance()->GetItemLinkByLocale($locale));
		} else {
			return str_replace("%s", $object->GetSpellId(), Wowhead::GetInstance()->GetSpellLinkByLocale($locale));
		}
	}
	
	function Reagents($craft)
	{
		$string = null;
		
		$string .= "<table cellspacing='0'>";
		
		foreach ( $craft->GetReagents() as $id => $quantity ) {
			$object = ObjectContainer::GetInstance()->GetItem($id);
			$object_img = file_exists(dirname(__FILE__)."/../".$object->GetTexture().".png") ? $object->GetTexture() : $this->path_to_question_mark;
			
			$string .= "<tr>";
			
			$string .= "<td style='vertical-align: top;'>".$quantity."x </td>";
			$string .= "<td style='vertical-align: top;'><img src='' title='".$object_img.".png' alt='' style='height: 15px; vertical-align: middle;' /></td>";
			$string .= "<td>";
			
			if ( $craft->HasReagentsThisLocale() ) {
				$string .= "<a href='".$this->GetObjectLink($object, true, SessionRoster::GetInstance()->GetLocalization())."'><span style='color: #".$object->GetColor().";' >".utf8_encode($object->GetNameThisLocale())."</span></a>";
			}
			
			$has_reagents_other_locales = false;
			
			foreach ( $craft->GetReagentsOtherLocales() as $locale ) {
				if ($has_reagents_other_locales == false) {
					$has_reagents_other_locales = true;
					$string .= "<em>(";
				}
				
				$string .= "<a href='".$this->GetObjectLink($object, true, $locale)."'>";
				$string .= "<span style='color: #".$object->GetColor()."; font-style: italic;' ";
				$string .= ">";
				$string .= utf8_encode($object->GetNameLocale($locale));
				$string .= "</span>";
				$string .= "</a>";
				
				$string .= ",";
			}
			
			if ($has_reagents_other_locales == true) {
				$string = substr($string, 0, strlen($string)-1);
				$string .= ")</em>";
			}
			
			$string .= "</td>";
			$string .= "</tr>";
		}
		
		$string .= "</table>";
		
		return $string;
	}
	
	function Characters($craft)
	{
		$string = null;
		
		if ( count($craft->GetCharacters()) > $this->reagent_max_characters_default )
		{
			$string .= utf8_encode(implode( ", ", array_slice($craft->GetCharacters(), 0, $this->reagent_max_characters_default)));
			$string .= ", ";
			
			$string .= "<span id='".$craft->GetUniqueId()."_more' onclick=\"more_less('".$craft->GetUniqueId()."');\">";
			$string .= "(";
			$string .= "<em><a href='javascript:;'>".Localization::GetInstance()->Get('more...')."</a></em>";
			$string .= ")";
			$string .= "</span>";
			
			$string .= "<span id='".$craft->GetUniqueId()."_chars' style='display: none;'>";
			$string .= utf8_encode(implode( ", ", array_slice($craft->GetCharacters(), $this->reagent_max_characters_default, count($craft->GetCharacters()) - $this->reagent_max_characters_default)));		
			$string .= ". ";
			$string .= "</span>";
			
			$string .= "<span id='".$craft->GetUniqueId()."_less' onclick=\"more_less('".$craft->GetUniqueId()."');\" style='display: none;'>";
			$string .= "(";
			$string .= "<em><a href='javascript:;' style='color: rgb(190, 0, 0);'>".Localization::GetInstance()->Get('less...')."</a></em>";
			$string .= ")";
			$string .= "</span>";
		}
		else
		{
			$string .= utf8_encode(implode(", ", $craft->GetCharacters()).".");
		}
		
		return $string;
	}
}

?>